package com.ruoyi.project.task.transfer.service;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.task.mission.domain.TaskMission;
import com.ruoyi.project.task.mission.domain.TaskMissionVo;
import com.ruoyi.project.task.mission.mapper.TaskMissionMapper;
import com.ruoyi.project.task.postpone.domain.TaskPostpone;
import com.ruoyi.project.task.postpone.mapper.TaskPostponeMapper;
import com.ruoyi.project.task.transfer.domain.TaskTransfer;
import com.ruoyi.project.task.transfer.mapper.TaskTransferMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Pathosis
 * @date 2020-09-19 14:32
 * @description:
 */
@Service
public class TaskTransferServiceImpl implements ITaskTransferService{
    @Autowired
    private TaskTransferMapper ttm;

    @Autowired
    private TaskMissionMapper tmm;

    @Autowired
    private TaskPostponeMapper tpm;

    @Override
    public List<TaskTransfer> selectTaskTransferList(TaskTransfer taskTransfer) {
        //展示isShow = 1
        taskTransfer.setIsShow(UserConstants.TRANSFER_IS_SHOW);
        return ttm.selectAllTaskTransfer(taskTransfer);
    }

    /**
     * taskTransfer 对象
     *  新添加的话，将taskmission对象的infoId的值改为toId的值
     * @param taskTransfer
     * @return
     */
    @Override
    public int insertTaskTransfer(TaskTransfer taskTransfer) {
        taskTransfer.setAuditState(UserConstants.TRANSFER_RATIFY_CODE);
        taskTransfer.setIsShow(UserConstants.TRANSFER_IS_SHOW);
        taskTransfer.setCreateBy(ShiroUtils.getLoginName());
        return ttm.insertTaskTransfer(taskTransfer);
    }

    @Override
    public int deleteTaskPostponeByIds(String ids) {
        Long[] transferId = Convert.toLongArray(ids);
        return ttm.deleteTaskPostponeByIds(transferId);
    }

    @Override
    public TaskTransfer selectTaskTransferById(Long transferId) {
        return ttm.selectTaskTransferById(transferId);
    }

    @Override
    public int updateTaskTransfer(TaskTransfer taskTransfer) {
        taskTransfer.setUpdateBy(ShiroUtils.getLoginName());
        return ttm.updateTaskTransfer(taskTransfer);
    }

    @Override
    public int updateTaskTransferByRatifyIsSuccess(TaskTransfer taskTransfer) {
        //是否同意，不同意则不走下面，调用上面的修改
        if(UserConstants.TRANSFER_NOT_SUCCESS.equals(taskTransfer.getAuditState())){
           return updateTaskTransfer(taskTransfer);
        }
        //审批通过后 ， 修改其他表的任务执行人(执行人id为当前的toId)，转移表的当前id数据隐藏，转移表添加一个用于显示的字段，查询只查询可以显示的字段
        if(taskTransfer.getToId() != null && taskTransfer.getMissionId() != null){
            //修改任务表中的执行人 info_id -> toId
            taskTransfer.setUpdateBy(ShiroUtils.getLoginName());
            TaskMission taskMission = new TaskMission();
            taskMission.setInfoId(taskTransfer.getToId());
            taskMission.setMissionId(taskMission.getMissionId());
            tmm.updateTaskMission(taskMission);

            //将延期表中的任务执行人替换,先通过missionId和infoId去查询对应的TaskPostpone对象
            TaskPostpone tp = new TaskPostpone();
            tp.setInfoId(taskTransfer.getInfoId());
            tp.setMissionId(taskTransfer.getMissionId());

            tp = tpm.selectTaskPostponeByMissionIdAndInfoId(tp);
            tp.setInfoId(taskTransfer.getToId());

            tpm.updateTaskPostpone(tp);
        }

        //批准修改是否展示         展示isShow = 0
        taskTransfer.setIsShow(UserConstants.TRANSFER_NOT_IS_SHOW);
        return updateTaskTransfer(taskTransfer);
    }

}
